package com.example.message.web;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.example.common.Ov.LoginOv;
import com.example.common.entity.Rsp;
import com.example.common.util.AuthenticationUtil;
import com.example.message.service.RedisServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
public class MessageCodeController {

    @Autowired
    RedisServiceImpl redisService;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @PostMapping("/getMessageCode")
    public Rsp getMessageCode(@RequestBody LoginOv loginOV){

        //System.out.println(loginOV.toString());
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI4Fz8SxbGd1NsZFk9AXpp", "KfsI9xVzQo4UxXZ6BlXoxbCiecuhZy");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", loginOV.getMobile());
        request.putQueryParameter("SignName", "武汉力雷");
        request.putQueryParameter("TemplateCode", "SMS_205457265");
        request.putQueryParameter("TemplateParam", "{\"code\":\""+loginOV.getVerification()+"\"}");
        CommonResponse response = null;
        try {
            response = client.getCommonResponse(request);
            System.out.println(response.getData());
            //验证码发送成功，将手机号和验证码存入redis
            // long time = 180L;

            stringRedisTemplate.opsForValue().set(loginOV.getMobile(), loginOV.getVerification(),3, TimeUnit.MINUTES);
//            boolean result = redisService.set(loginOV.getPhone(), loginOV.getMessageCode(), time);
            return new Rsp(0,loginOV,"验证码发送成功！");
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return new Rsp(1,"验证码发送失败！");
    }

    //身份证识别
    @PostMapping("/authentication")
    public Rsp authentication(@RequestBody LoginOv loginOv){
        System.out.println(loginOv.getMobile());
        String appcode = "6452108bc9ad47f3b0becce0fd73cad9";
        String url = "http://shenfenzhe.market.alicloudapi.com/do";
        String imageurl = loginOv.getMobile();
        String params= "image="+imageurl+"&id_card_side=front";

        //发送 POST 请求
        String sr= AuthenticationUtil.sendPost(url, params,appcode);
        System.out.println(sr);
        Object parse = JSON.parse(sr);
        System.out.println(parse.toString());
        return new Rsp(0,parse,"识别成功");
    }
}
